// pages/my/wallet/wallet.js
import Config from "../../common/config.js";
var sessionId = wx.getStorageSync('sessionId');
console.log(sessionId);
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    money: "20",
    timeStamp: "",
    nonceStr: "",
    package: "",
    paySign: "",
    yue:""
  },
  yue:function(){
    let obj = this;
    //调取可用余额接口
    wx.request({
      url: Config.url + '/api/pay/get_balance',
      method: "POST",
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      success: function (res) {
        // console.log(res.data.code);
        obj.setData({
          yue: res.data.data.balance
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let obj = this;
    //调取可用余额接口
    wx.request({
      url: Config.url + '/api/pay/get_balance',
      method: "POST",
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      success: function (res) {
        // console.log(res.data.code);

        obj.setData({
          yue: res.data.data.balance
        });
        if(res.data.code == 600){
          wx.request({
            url: 'https://zhangyushu.liangziqishi.shop/api/app/login', //接口地址
            data: { code: res.code},
            method: 'POST',
            header: {
              'content-type': 'application/json' //默认值
            },
            success: function (res) {
              console.log(res.data);
              wx.setStorageSync("session_key", res.data.data.data.session_key);
              // wx.setStorageSync("sessionId",res.data.data.data.sessionId);
              wx.setStorageSync("openid", res.data.data.data.openid)
              wx.setStorage({
                key: 'sessionId',
                data: res.data.data.data.sessionId,
              });
              obj.yue()
            }

          })
          // ------
        }
      }
    })
  },
  goDetails: function() {
    wx.navigateTo({
      url: '../walletList/list'
    })
  },
  changeOil: function(e) {
    console.log(e.target.dataset.num);
    this.setData({
      num: e.target.dataset.num
    });
    if (e.target.dataset.num==2){
      this.setData({
        money:"50"
      })
    } else if (e.target.dataset.num == 3){
      this.setData({
        money: "100"
      })
    } else if (e.target.dataset.num == 4){
      this.setData({
        money: "200"
      })
    } else if (e.target.dataset.num == 5) {
      this.setData({
        money: "500"
      })
    } else if (e.target.dataset.num == 6) {
      this.setData({
        money: "1000"
      })
    }
  },
  gopaymoney: function() {
    var obj = this;
    var sessionId = wx.getStorageSync('sessionId')
    wx.request({
        url: Config.url + '/api/pay/pre_wallet_order',
        method: "POST",
        header: {
          'content-type': 'application/json',
          'accessToken': sessionId
        },
        data: {
          // money: obj.data.money,
          money:"0.01"
          // openId: "oebPD5Fp-3_WI_aeRKsPbnLyKwAs"
        },
        success: function(res) {
          console.log(res.data.data.mapResult);
          // obj.setData({
          //   timeStamp: res.data.data.mapResult.timeStamp,
          //   nonceStr: res.data.data.mapResult.nonceStr,
          //   package: res.data.data.mapResult.package,
          //   paySign: res.data.data.mapResult.paySign,
          // });
          wx.requestPayment({
            'timeStamp': res.data.data.mapResult.timeStamp,
            'nonceStr': res.data.data.mapResult.nonceStr,
            'package': res.data.data.mapResult.package,
            'signType': res.data.data.mapResult.signType,
            'paySign': res.data.data.mapResult.paySign,
            'success': function (res) {
              wx.showToast({
                title: '支付成功',
              })
              obj.yue();
            },
            'fail': function (res) {
              wx.showToast({
                title: '支付失败',
              })
            },
            'complete': function (res) {
              console.log("---------", res)
            }
          });
        }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.yue();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.yue();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log("页面下拉");
    this.yue();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})